Ext.onReady(function() {
	var toolbar = Ext.create('Ext.toolbar.Toolbar', {
		dock : 'top',
		items : [{
			text : '返回',
			xtype : 'button',
			listeners:{
				click: function(){
					location.href="/wuziMgr/device_mgr";
				}
			}
		}, '-' ,{
			text : '删除',
			xtype : 'button',
			listeners:{
				click: function(){
					var grid = Ext.getCmp("wzzt_grid");
					var selectionModel = grid.getSelectionModel();
					var models = selectionModel.getSelection();
					var ids = [];
					for(var i=0; i<models.length;i++){
						ids[i] = models[i].get("Id");
					}
					Ext.Ajax.request({
						url: '/device_status/delete',
						type: 'POST',
						params:{
							ids: ids
						},
						callback:function(options,success, response){
							if(success){
								var result  = Ext.decode(response.responseText);
								if(result.success){
									Ext.getCmp("wzzt_grid").getStore().reload();
								}
								else{
									Ext.Msg.alert('错误', result.msg);
								}
							}
						}
					});
				}
			}
		},{
			text : '刷新',
			xtype : 'button',
			listeners:{
				click: function(){
					Ext.getCmp("wzzt_grid").getStore().reload();
				}
			}
		},'-', {
			id: 'add_wzzt_form',
			xtype: 'form',
			layout: 'hbox',
			items: [{
				xtype:'label',
				margin: '5 5',
				text: '名称:'
			},{
				xtype:'textfield',
				allowBlank:false,
				emptyText: '名称',
				id: 'wzzt_name'
			},{
				xtype: 'label',
				margin: '5 5',
				text: '描述'
			},{
				xtype: 'textfield',
				id: 'wzzt_desp'
			}]
		},{
			xtype: 'button',
			text:'添加',
			listeners:{
				click:function(){
					if(!Ext.getCmp('add_wzzt_form').isValid()){
						return;
					}
					var wzztName = Ext.getCmp("wzzt_name");
					var wzztDesp = Ext.getCmp("wzzt_desp");
					Ext.Ajax.request({
						url: '/device_status/add',
						params:{
							Name: wzztName.getValue(),
							Desp: wzztDesp.getValue()
						},
						callback: function(options, success, response){
							if(success){
								var result = Ext.decode(response.responseText);
								
								if(result.success){
									wzztName.setValue('');
									wzztName.clearInvalid();
									wzztDesp.setValue('');
									Ext.getCmp("wzzt_grid").getStore().reload();
								}
								else{
									Ext.Msg.alert('错误', result.msg);
								}
							}
						}
					})
					
				}
			}
		}]
	});
	
	var store = Ext.create('Ext.data.Store', {
	    fields:['Id', 'Name', 'Desp'],
	    autoLoad:true,
	    pageSize: 25,
	    proxy: {
	        type: 'ajax',
	        url: '/device_status/all',
	        reader: {
	            type: 'json',
	            root: 'data.rows',
	            totalProperty: 'data.total'
	        }
	    }
	});
	
	var grid = Ext.create('Ext.grid.Panel', {
		id: "wzzt_grid",
		selType : 'checkboxmodel',
		store: store,
		margin: '5 5',
		columns : [ {
			text : '序号',
			xtype : 'rownumberer',
			width : 40,
			sortable : false
		}, {
			text : '名称',
			dataIndex : 'Name'
		}, {
			text : '描述',
			dataIndex : 'Desp',
			flex:1
		}, {
			text: "详细",
			renderer:function(v, meta, record){
				return '<a href="device_status/detail/'+record.get("Id")+'">详细</a>';
			}
		}],
		bbar : Ext.create('Ext.PagingToolbar', {
			store : store,
			border: false,
			displayInfo : true,
			displayMsg : '显示{0} - {1} / 总数 {2}',
			emptyMsg : "没数据"
		})
	});
	
	var panel = Ext.create("Ext.panel.Panel", {
		title: '物资状态',
		border : false,
		bodyBorder : false,
		layout : 'fit',
		dockedItems : [ toolbar ],
		items : [grid]
	});
	Ext.create('Ext.Viewport', {
		layout : 'fit',
		style : 'background:none;background-color:white;',
		items : [ panel ]
	});
});